package com.pipayshop.service.impl;

import com.pipayshop.domain.StoreCategory;
import com.pipayshop.domain.StoreCategoryTop;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.mapper.PhysicalStoreCategoryMapper;
import com.pipayshop.service.PhysicalStoreCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.mapper.ImageMapper;
import com.pipayshop.utils.Constants;
import com.pipayshop.utils.RedisUtil;
import com.pipayshop.utils.RedissonUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实体店二级分类表 服务实现类
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Service
public class PhysicalStoreCategoryServiceImpl extends ServiceImpl<PhysicalStoreCategoryMapper, StoreCategory> implements PhysicalStoreCategoryService {
    @Resource
    private PhysicalStoreCategoryMapper shopCategoryMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private RedisUtil redisUtil;
    /**
     * 查询一级分类对应的二级分列表
     */
    @Override
    public PageDataVO getShopCategoryList(Integer pageNum, Integer pageSize, Integer topId) {
        LambdaQueryWrapper<StoreCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreCategory::getDelFlag, 0)
                .eq(StoreCategory::getCategoryPid, topId);
        Page<StoreCategory> page = new Page<>(pageNum, pageSize);
        Page<StoreCategory> selectPage = shopCategoryMapper.selectPage(page, wrapper);
        return new PageDataVO(Integer.valueOf(selectPage.getTotal() + ""), selectPage.getRecords());
    }

    /**
     * 根据分类id查询分类
     */
    @Override
    public StoreCategory getShopCategoryById(String categoryId) {
        return shopCategoryMapper.selectOne(new LambdaQueryWrapper<StoreCategory>()
                .eq(StoreCategory::getCategoryId, categoryId)
                .eq(StoreCategory::getDelFlag, 0));
    }

    /**
     * 根据分类id删除分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteShopCategoryById(String categoryId) {
        return shopCategoryMapper.update(null, new LambdaUpdateWrapper<StoreCategory>()
                .eq(StoreCategory::getDelFlag, 0)
                .eq(StoreCategory::getCategoryId, categoryId)
                .set(StoreCategory::getDelFlag, 1)) > 0;
    }

    /**
     * 查询二级分类列表
     */
    @Override
    public List<StoreCategory> getShopCategorySecList(String categoryPid) {
        List<StoreCategory> storeCategories = RedissonUtils.getCacheList(Constants.PHYSICAL_CATEGORY_SEC_LIST_KEY+categoryPid);
        // 校验缓存结果
        if (!storeCategories.isEmpty()) {
            return storeCategories;
        }
        List<StoreCategory> shopCategories = shopCategoryMapper.getShopCategoryByCategory(categoryPid);
        RedissonUtils.setCacheList(Constants.PHYSICAL_CATEGORY_SEC_LIST_KEY+categoryPid, shopCategories, Duration.ofDays(1));
        return shopCategories;
    }

}
